package com.oreilly.android.taskmanager.adapter;

import java.util.ArrayList;

import com.oreilly.android.taskmanager.R;
import com.oreilly.android.taskmanager.tasks.Task;
import com.oreilly.android.taskmanager.views.TaskListItem;

import android.content.Context;
import android.location.Location;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class TaskListAdapter extends BaseAdapter {
	private static final long LOCATION_FILTER_DISTANCE = 200;
	private ArrayList<Task> allTasks;
	private ArrayList<Task> filteredTasks;
	private Context context;

	public TaskListAdapter(ArrayList<Task> tasks, Context context) {
		super();
		this.allTasks = tasks;
		this.filteredTasks = tasks;
		this.context = context;
	}

	public int getCount() {
		return filteredTasks.size();
	}

	public Task getItem(int position) {
		return (null == filteredTasks) ? null : filteredTasks.get(position);
	}

	public long getItemId(int position) {
		return getItem(position).getId();
	}

	public void addTask(Task t) {

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TaskListItem tli;
		if (null == convertView) {
			tli = (TaskListItem) View.inflate(context, R.layout.task_list_item,
					null);
		} else {
			tli = (TaskListItem) convertView;
		}
		tli.setTask(filteredTasks.get(position));

		return tli;
	}

	public void setFilter(String string) {
		if (string.equals("All")) {
			filteredTasks = allTasks;
		} else if (string.equals("Completed")) {
			filteredTasks = filterCompletedTasks();
		} else if (string.equals("Local")) {
			filteredTasks = filterLocalTasks();
		} else if (string.equals("Due Soon")) {
			filteredTasks = filterDueSoonTasks();
		}
		notifyDataSetChanged();
	}

	public void toggleTaskCompleteAtPosition(int position) {
		Task t = getItem(position);
		t.toggleComplete();
		notifyDataSetChanged();
	}

	public Long[] removeCompletedTasks() {
		ArrayList<Long> completedIds = new ArrayList<Long>();
		ArrayList<Task> completedTasks = new ArrayList<Task>();

		for (Task t : allTasks) {
			if (t.isComplete()) {
				completedIds.add(t.getId());
				completedTasks.add(t);
			}
		}

		allTasks.removeAll(completedTasks);
		notifyDataSetChanged();
		completedIds.toArray();
		return completedIds.toArray(new Long[] {});
	}

	public void filterTasksByLocation(Location latestLocation,
			long locationFilterDistance) {
		ArrayList<Task> filteredTasks = new ArrayList<Task>();

		this.filteredTasks = filteredTasks;
		notifyDataSetChanged();
	}

	// private boolean taskIsWithinGeofence(Task t, Location latestLocation,
	// long locationFilterDistance) {
	// float[] distanceArray = new float[1];
	// if (latestLocation == null) {
	// return false;
	// }
	// Location.distanceBetween(t.getLatitude(), t.getLongitude(),
	// latestLocation.getLatitude(), latestLocation.getLongitude(),
	// distanceArray);
	//
	// return distanceArray[0] < locationFilterDistance;
	// }

	private ArrayList<Task> filterCompletedTasks() {
		ArrayList<Task> tasks = new ArrayList<Task>();

		for (Task t : allTasks) {
			if (!t.isComplete()) {
				tasks.add(t);
			}
		}

		return tasks;
	}

	private ArrayList<Task> filterDueSoonTasks() {
		ArrayList<Task> tasks = new ArrayList<Task>();
		for (Task t : allTasks) {
			// if the task's due date is within 5 days, add to list
		}
		return tasks;
	}

	private ArrayList<Task> filterLocalTasks() {
		ArrayList<Task> tasks = new ArrayList<Task>();
		// for (Task t : allTasks) {
		// if (t.hasLocation()
		// && taskIsWithinGeofence(t, latestLocation,
		// LOCATION_FILTER_DISTANCE)) {
		// filteredTasks.add(t);
		// }
		// }
		return tasks;
	}
}
